package admin

import (
	"fmt"
	"strconv"
	"strings"
	"tcsc/models"
	"tcsc/sword"

	"github.com/astaxie/beego"
)

type News struct {
	BaseController
}

func (c *News) Get() {
	c.Data["column"] = models.Query(`select id,name from mt_column where type='n' and status=1 and pid=0`)
	page, page2, num := models.Pagechange(c.GetString("page"), "")
	var w []string
	w = append(w, `a.status=1`)
	cid := c.GetString("cid")
	c.Data["cid"] = cid
	search := c.GetString("search")
	if search != "" {
		w = append(w, `(a.title like '%`+search+`%'`)
	} else {
		if cid != "" {
			w = append(w, `a.cid=`+cid)
		}
	}
	ws := strings.Join(w, ` and `)
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.sort desc,a.addtime desc`
	sql := `select a.*,b.name as cname,c.pathname from mt_news a left join mt_column b on a.cid=b.id left join mt_resources c 
	on a.img=c.id where ` + ws + ob + lt
	sql2 := `select count(a.id) as totals from mt_news a where ` + ws
	info := models.Query(sql)
	for _, v := range info {
		keywords := fmt.Sprint(v["keywords"])
		keywords = strings.Trim(keywords, ",")
		if keywords == "" {
			keywords = "''"
		}
		v["keywords"] = models.Query(fmt.Sprintf(`select id,title from mt_keywords where id in(%v)`, keywords))
	}
	c.Data["info"] = info
	c.Data["paginator"] = models.Page(sql2, page2, num)
	c.Data["url"] = `/admin/news/index?cid=` + cid + `&search=` + search + `&`
	c.TplName = "admin/news/index.html"
}

type NewsAE struct {
	BaseController
}

func (c *NewsAE) Get() {
	c.Data["column"] = models.Query(`select id,name from mt_column where type='n' and status=1 and pid=0`)
	c.Data["keywords"] = models.Query(`select id,title from mt_keywords where type=1 and status=1`)
	id := c.GetString("id")
	if id != "" {
		if info := models.Query(`select a.*,b.pathname from mt_news a left join mt_resources b on a.img=b.id where a.id=` + id); info != nil {

			c.Data["info"] = info[0]
		} else {
			c.Ctx.Redirect(302, "/admin/news/index")
		}
	}
	c.TplName = "admin/news/ae.html"
}
func (c *NewsAE) Post() {
	defer c.ServeJSON()
	input := c.Input()
	input.Set("content", sword.CkeditorChange(c.GetString("content")))
	input.Set("keywords", strings.Replace(strings.Trim(strings.Join(c.GetStrings("keywords"), ","), ","), ",,", ",", -1)+",")
	if input.Get("id") == "" {
		input.Set("addtime", sword.Gettimes("2006-01-02 15:04:05"))
	}
	if res, err := models.Save("mt_news", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
		var r models.Mt_systems_record
		r.Tid = int(res)
		if input.Get("id") == "" {
			r.Way = 2
		} else {
			r.Way = 3
		}
		r.Addtime = sword.Timeget()
		r.Table = 21
		r.Uid, _ = strconv.Atoi(c.Qid)
		if _, err := models.Orm().Insert(&r); err != nil {
			beego.Debug(err)
		}
	}
}

type NewsContent struct {
	BaseController
}

func (c *NewsContent) Get() {
	id := c.GetString("id")
	if id != "" {
		if info := models.Query(`select a.*,b.pathname,c.pathname as pathname2 from mt_news a left join mt_resources b on a.img=b.id left join mt_resources c 
		on a.video=c.id where a.id=` + id); info != nil {
			for _, v := range info {
				keywords := fmt.Sprint(v["keywords"])
				keywords = strings.Trim(keywords, ",")
				if keywords == "" {
					keywords = "''"
				}
				v["keywords"] = models.Query(fmt.Sprintf(`select id,title from mt_keywords where id in(%v)`, keywords))
			}
			c.Data["info"] = info[0]
		} else {
			c.Ctx.Redirect(302, "/admin/news/index")
		}
	} else {
		c.Ctx.Redirect(302, "/admin/news/index")
	}
	c.TplName = "admin/news/content.html"
}

type NoviceCourse struct {
	BaseController
}

func (c *NoviceCourse) Get() {
	search := c.GetString("search")
	cid := c.GetString("cid")
	page, page2, num := models.Pagechange(c.GetString("page"), "")
	gid := c.Gid
	w := ` a.status=1 and a.gid=` + gid
	o := ` order by a.sort desc, a.modifytime desc`
	l := ` limit ` + page + `, ` + num
	if search != "" {
		w += ` and a.title like '%` + search + `%'`
	}
	if cid != "" {
		w += ` and a.cid=` + cid
	}
	ctn := models.Query(`select a.id,a.title,a.intro,a.addtime,a.modifytime,a.censoring from mt_novice_join a 
		where` + w + o + l)
	sql := `select count(a.id) as totals from mt_novice_join a where` + w
	column := models.Query(`select id, name from mt_novice_column where gid=? and status=1`, c.Gid)
	if column != nil {
		c.Data["column"] = column
	}
	c.Data["cid"] = cid
	c.Data["info"] = ctn
	c.Data["paginator"] = models.Page(sql, page2, num)
	c.Data["url"] = "/admin/novice/course?" + "search=" + search + "&cid=" + cid
	c.TplName = "admin/news/novice.html"
}

type NoviceCourseAE struct {
	BaseController
}

func (c *NoviceCourseAE) Get() {
	id := c.GetString("id")
	column := models.Query(`select id, name from mt_novice_column where gid=? and status=1`, c.Gid)
	if id != "" {
		info := models.Query(`select id,title,intro,content,sort,censoring,click,cid from mt_novice_join where id=?`, id)
		c.Data["info"] = info[0]
	}
	if column != nil {
		c.Data["column"] = column
	}
	c.TplName = "admin/news/novice_ae.html"
}

func (c *NoviceCourseAE) Post() {
	defer c.ServeJSON()
	js := make(map[string]interface{})
	c.Data["json"] = &js
	id := c.GetString("id")
	input := c.Input()
	input.Set("content", sword.CkeditorChange(c.GetString("content")))
	if id == "" {
		input.Set("addtime", sword.Gettimes("2006-01-02 15:04:05"))
		input.Set("gid", c.Gid)
	}
	input.Set("modifytime", sword.Gettimes("2006-01-02 15:04:05"))
	if _, err := models.Save("mt_novice_join", input); err != nil {
		beego.Debug(err)
		js["status"] = 0
	} else {
		js["status"] = 1
	}
}

type NoviceColumn struct {
	BaseController
}

func (c *NoviceColumn) Get() {
	gid := c.Gid
	page, page2, num := models.Pagechange(c.GetString("page"), "")
	search := c.GetString("search")
	w := ` a.status=1 and a.gid=` + gid
	if search != "" {
		w += ` and a.name like '%` + search + `%'`
	}
	o := ` order by a.sort desc, a.modifytime desc`
	l := ` limit ` + page + `, ` + num
	info := models.Query(`select a.id,a.name,a.addtime,a.modifytime,a.sort,b.pathname from mt_novice_column a 
		left join mt_resources b on a.img=b.id 
		where ` + w + o + l)
	sql := `select count(a.id) as totals from mt_novice_column a where ` + w
	c.Data["count"] = models.Query(`select count(a.id) as totals from mt_novice_column a where ` + w)[0]["totals"]
	c.Data["info"] = info
	c.Data["paginator"] = models.Page(sql, page2, num)
	c.Data["url"] = "/admin/novice/column?" + "search=" + search + "&"
	c.TplName = "admin/news/novice_column.html"
}

type NoviceColumnAE struct {
	BaseController
}

func (c *NoviceColumnAE) Get() {
	id := c.GetString("id")
	if id != "" {
		info := models.Query(`select a.id,a.name,a.sort,a.img,b.pathname from mt_novice_column a 
			left join mt_resources b on a.img=b.id 
			where a.id=?`, id)
		c.Data["info"] = info[0]
	}
	c.TplName = "admin/news/novice_column_ae.html"
}

func (c *NoviceColumnAE) Post() {
	js := make(map[string]interface{})
	js["status"] = "1"
	input := c.Input()
	id := input.Get("id")
	if id == "" {
		input.Set("addtime", sword.Gettimes(""))
		input.Set("gid", c.Gid)
	} else {
		input.Set("modifytime", sword.Gettimes(""))
	}
	if _, err := models.Save("mt_novice_column", input); err != nil {
		beego.Debug(err)
		js["status"] = "0"
	}
	c.Data["json"] = js
	c.ServeJSON()
}
